assert: fix TypeError on deepStrictEqual with null Map key or Set member#64449
Open
semx wants to merge 1 commit into
Open
assert: fix TypeError on deepStrictEqual with null Map key or Set member#64449semx wants to merge 1 commit into
semx wants to merge 1 commit into
Conversation
deepStrictEqual() and util.isDeepStrictEqual() threw "Cannot read properties of null (reading 'constructor')" instead of comparing when a Map key or Set member was null/undefined (or another primitive) and lined up against object-only keys/members in the other collection with an equal count. The primitive/null handling was gated behind an optimization that is skipped when the counts match, letting such keys reach objectComparisonStart, which dereferences `.constructor`. Resolve primitive and null keys/members directly in every case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
assert.deepStrictEqual()(andutil.isDeepStrictEqual()) throw aTypeErrorinstead of comparing when the firstMaphas anull(or other primitive) key that lines up against object-only keys in the other map:The same happens for an
undefinedkey.Sethas the identical problem for anull/undefinedmember (once the set is large enough to skip the small-set fast path):It only triggers in strict mode when the other collection's keys/members are all objects and their count equals the first collection's size.
Cause
In
mapObjectEquivandsetObjectEquiv(lib/internal/util/comparisons.js), primitive/nullkeys and members are resolved directly viab.has()/b.get(), but that handling was gated behindextraChecks(array.length !== a.size). When the counts match, the gate is skipped and the primitive/nullkey/member falls through toobjectComparisonStart, which dereferences.constructorand throws onnull/undefined.Fix
Handle primitive/
nullkeys and members unconditionally — they can only match by identity and can never match through the object comparator — so they are always resolved by direct lookup and never reachobjectComparisonStart. The collections above now compare as unequal (throwing anAssertionError, as expected) instead of throwing aTypeError. Object comparison is unchanged.Added regression cases (
nullandundefinedkeys/members, for bothMapandSet) totest/parallel/test-assert-deep.js.